package cn.kgc.controller;

import cn.kgc.service.ExcelService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author shkstart
 * @create 2020-04-07 8:31
 */
@Controller
public class ExcelController {

    @Resource
    ExcelService excelService;


    @RequestMapping(value = "/importExcel",method = RequestMethod.POST)
    @ResponseBody
    public String importExcel(@RequestParam("excelFile") MultipartFile excelFile){
        if(excelFile!=null){
            if(!excelFile.getOriginalFilename().matches("^.+\\.(?i)((xls)|(xlsx))$")){
                return "请上传后缀名为xls或xlsx的Excel文件!";
            }
            try {
                if(excelService.importExcel(excelFile)==1){
                    return "excel上传成功!";
                }else {
                    return "excel上传失败";
                }



            }catch (InvalidFormatException |IOException e){
                e.printStackTrace();
                return "excel上传失败";
            }
        }
        return "excel上传失败";
    }



}
